package com.cv4j.app.fragment;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.ability.CompareHistAbility;
import com.cv4j.app.ability.HistogramDemoAbility;
import com.cv4j.app.ability.HistogramEqualizationAbility;
import com.cv4j.app.ability.ProjectHistAbility;
import com.cv4j.app.app.BaseFragment;
import com.cv4j.app.utils.ClickUtil;

/**
 * 直方图
 *
 * @since 2021-05-14
 */
public class HistFragment extends BaseFragment {
    private Context mContext;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_hist, container, false);
        mContext = container.getContext();
        initView(root);
        return root;
    }

    private void initView(Component root) {
        root.findComponentById(ResourceTable.Id_text1).setClickedListener(component -> {
            jumpToAbility(HistogramEqualizationAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text2).setClickedListener(component -> {
            jumpToAbility(HistogramDemoAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text3).setClickedListener(component -> {
            jumpToAbility(CompareHistAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text4).setClickedListener(component -> {
            jumpToAbility(ProjectHistAbility.class, (Text) component);
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    /**
     * jumpToAbility
     *
     * @param toClass toClass
     * @param text text
     */
    public void jumpToAbility(Class toClass, Text text) {
        if (ClickUtil.isFastClick()) {
            return;
        }
        if (mContext == null) {
            throw new NullPointerException("you are context is null");
        }
        Intent intent = new Intent();
        intent.setParam("Title", text.getText());
        intent.setOperation(new Intent.OperationBuilder()
            .withBundleName(mContext.getBundleName())
            .withAbilityName(toClass.getName())
            .build());

        // 换成下面context调用
        mContext.startAbility(intent, 0);
    }
}
